/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.wordlist;

import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Stream;
import zemberek.morphology.apps.TurkishMorphParser;
import zemberek.morphology.parser.MorphParse;

/**
 *
 * @author Revivius
 */
public class MainClass {

    public static void main(String[] args) throws Exception {
        URL url = MainClass.class.getResource("full.txt.tr");
        Path path = Paths.get(url.toURI());

        TurkishMorphParser mp = TurkishMorphParser.createWithDefaults();
        
        Set<String> stems = new TreeSet<>();
        Stream<String> lines = Files.lines(path);
        
        lines.map(s -> s.split(" ")[0]).forEach(s -> {
            String stem = findStem(mp, s);
            stems.add(stem);
        });
        
        System.out.println(stems.size());
        
        Path out = Paths.get("out");
        
        BufferedWriter writer = Files.newBufferedWriter(out, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        try (PrintWriter pw = new PrintWriter(writer)) {
            stems.stream().forEach(s -> pw.println(s));
        }
        
    }
    
    private static String findStem(TurkishMorphParser parser, String word) {
        List<MorphParse> mps = parser.parse(word);
       
        if (mps != null && !mps.isEmpty()) {
            MorphParse mp = mps.get(0);
            String s = mp.getStems().get(0);
            return s;
        }

        return "";
    }

}
